package com.yhh.common.base.ret.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yhh.common.base.ret.ApiResult;
import com.yhh.common.entity.member.model.User;
import com.yhh.common.jwt.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    @Autowired
    private HttpServletRequest request;
    /**
     * 获取当前登录的用户信息
     * @return
     */
    public User getCurrentUser(){
        String token = request.getHeader("TOKEN");
        ApiResult ret = JWTUtil.verifyToken(token);
        if(ret.getCode() != ApiResult.SUCCESS_CODE){
            return null;
        }
        JSONObject user = JSONObject.parseObject(ret.getResult().get("user").toString());
        return JSON.toJavaObject(user,User.class);
    }
}
